package com.sghj.utils.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.sghj.utils.smallMethods;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;


public class HeaderFooter  extends PdfPageEventHelper {
    public PdfTemplate tpl;
    public BaseFont bf;
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(550);
        table.setLockedWidth(true);
        try {
            //获取当前日期
            smallMethods smd=new smallMethods();
            String date=smd.getDate(new Date());
            table.addCell(createHeaderCell(date.replace("-","/")));
            table.addCell(createHeaderCell("招标管理系统"));
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
//
        table.writeSelectedRows(0, -1, 25, document.top() + 25,
                writer.getDirectContent());

//        BaseFont baseFontChinese = null;
//        try {
//            baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        Font fontChinese = new Font(baseFontChinese, 9, Font.NORMAL);
//        PdfContentByte cb=writer.getDirectContent();
//        cb.saveState();
//        cb.beginText();
//        cb.setFontAndSize(fontChinese.getBaseFont(),fontChinese.getSize());
//        // 获取当前页码和总页数
//        int pageNumber = writer.getPageNumber();
//        int totalPages = writer.getCurrentPageNumber();
//        String pageText = String.format("%d/%d", pageNumber, totalPages);
//        cb.showTextAligned(Element.ALIGN_RIGHT, String.valueOf(pageText), document.right(), document.bottom(), 0);
//        cb.endText();


        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();

//        String text = " Page " + writer.getPageNumber() + " of ";
        String text = writer.getPageNumber() + " / ";
        float textSize = bf.getWidthPoint(text, 9);
        float textBase = document.bottom()-8;
        cb.beginText();
        cb.setFontAndSize(bf, 9);
        // for odd pagenumbers, show t

        cb.setTextMatrix(document.right()-15, textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.right() + textSize-15, textBase);
        cb.restoreState();

    }

    private PdfPCell createHeaderCell(String text) throws DocumentException, IOException {
        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(baseFontChinese, 9, Font.NORMAL);
        PdfPCell cell = new PdfPCell(new Phrase(text,fontChinese));
//        cell.setBorderColor(BaseColor.RED);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_LEFT);
        cell.setBorderWidth(0f);
        return cell;
    }
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {

        try {
            // initialization of the template
            tpl = writer.getDirectContent().createTemplate(100, 100);

            // tpl.setBoundingBox(new Rectangle(0, 0, 10, 10));
            // initialization of the font
            bf = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
        } catch (Exception e) {

        }
    }


    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {

//关闭document的时候获取总页数，并把总页数按模版写道之前预留的位置

        tpl.beginText();
        tpl.setFontAndSize(bf, 9);
        tpl.setTextMatrix(0, 0);
        tpl.showText("" + (writer.getPageNumber()));
        tpl.endText();

    }
}
